package main

import (
	"context"
	"fmt"

	"codingq92.com/thrift_demo/gen/thrift/interfaces"
	"github.com/apache/thrift/lib/go/thrift"
)

func main() {
	// 1. 创建传输层（连接服务端）
	addr := "localhost:18000"
	transport := thrift.NewTSocketConf(addr, &thrift.TConfiguration{})
	// 2. 包装传输层（带缓冲的传输）
	transportFactory := thrift.NewTTransportFactory()
	tTransport, err := transportFactory.GetTransport(transport)
	if err != nil {
		panic(err)
	}
	defer func(transport *thrift.TSocket) {
		_ = transport.Close()
	}(transport)
	// 3. 打开连接
	if err := tTransport.Open(); err != nil {
		panic(fmt.Sprintf("打开连接失败: %v", err))
	}
	// 4. 设置协议（需与服务端一致）
	protocolFactory := thrift.NewTBinaryProtocolFactoryConf(&thrift.TConfiguration{})
	// 5. 创建客户端
	factory := interfaces.NewThriftDemoServiceClientFactory(tTransport, protocolFactory)
	world, err := factory.HelloWorld(context.Background(), "我是client啊!!!")
	if err != nil {
		panic(err)
	}
	fmt.Println(world)
}
